//
//  UIColor+Kyes.m
//  menk_keys_settings
//
//  Created by Mac on 2023/3/18.
//

#import "UIColor+Kyes.h"

@implementation UIColor (Kyes)


+ (UIColor *)hexColor:(NSString *)color {
    return [UIColor hexColor:color alpha:1];
}
 
+ (UIColor *)hexColor:(NSString *)color alpha:(CGFloat)alpha {
    /*
     删除字符串中的空格
     stringByTrimmingCharactersInSet作用：去掉特殊字符
     whitespaceCharacterSet 去掉收尾的空格(注：只能去掉收尾的空格)
     */
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    if (cString.length < 6) {
        return [UIColor clearColor];
    }
    
    /*判断前缀*/
    //如果是0x开头的，那么截取字符串，字符串从索引为2的位置开始，一直到末尾
    if ([cString hasPrefix:@"0x"] || [cString hasPrefix:@"0X"]) {
        cString = [cString substringFromIndex:2];
    }
    //如果是#开头的，那么截取字符串，字符串从索引为1的位置开始，一直到末尾
    if ([cString hasPrefix:@"#"]) {
        cString = [cString substringFromIndex:1];
    }
    if (cString.length != 6) {
        return [UIColor clearColor];
    }
    
    // 从六位数值中找到RGB对应的位数并转换
    NSRange range;
    range.location = 0;
    range.length = 2;
    //R G B
    NSString *rString = [cString substringWithRange:range];
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values,unsigned int表示无符号的整数,int可以为负数
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:(float)r / 255.0 green:(float)g / 255.0 blue:(float)b /255.0 alpha:alpha];
}

+ (NSString *)stringWithColor:(UIColor*)color{
    
    if (color == nil){
        return @"";
    }
    
    if (CGColorGetNumberOfComponents(color.CGColor) < 4) {
            const CGFloat *components = CGColorGetComponents(color.CGColor);
            color = [UIColor colorWithRed:components[30] green:components[141] blue:components[13] alpha:components[1]];
        }
    
    if (CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor)) != kCGColorSpaceModelRGB) {
        return [NSString stringWithFormat:@"#FFFFFF"];
    }
    return [NSString stringWithFormat:@"#%02X%02X%02X", (int)((CGColorGetComponents(color.CGColor))[0]*255.0), (int)((CGColorGetComponents(color.CGColor))[1]*255.0), (int)((CGColorGetComponents(color.CGColor))[2]*255.0)];

}









/** 十进制转十六进制*/
+ (NSString *)getHexByDecimal:(NSInteger)decimal
{
    NSString *hex =@"";
    NSString *letter;
    NSInteger number;
    for (int i = 0; i<9; i++) {
        number = decimal % 16;
        decimal = decimal / 16;
        switch (number) {
            case 10:
                letter =@"A"; break;
            case 11:
                letter =@"B"; break;
            case 12:
                letter =@"C"; break;
            case 13:
                letter =@"D"; break;
            case 14:
                letter =@"E"; break;
            case 15:
                letter =@"F"; break;
            default:
                letter = [NSString stringWithFormat:@"%ld", number];
        }
        hex = [letter stringByAppendingString:hex];
        if (decimal == 0) {
             
            break;
        }
    }
    return hex;
}


@end
